

package com.jinwei.domino;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

import lotus.domino.*;
import org.apache.commons.lang.StringUtils;

public class DominoUtils {
    final static String DEFAULT_SPLIT_WORD = ";";

    public static Database createDatabase(String ip, String dbFilePath, Database dbTemplate) {
        if (dbTemplate == null) {
            return null;
        } else if (StringUtils.isBlank(dbFilePath)) {
            return null;
        } else {
            Database db = null;

            try {
                if (StringUtils.isBlank(ip)) {
                    db = dbTemplate.createCopy("", dbFilePath);
                } else {
                    db = dbTemplate.createCopy(ip, dbFilePath);
                }
            } catch (NotesException var5) {
                var5.printStackTrace();
            }

            return db;
        }
    }

    public static void executeConsole(String command) {
        Session session = null;
        NotesThread.sinitThread();

        try {
            session = NotesFactory.createSession();
            session.sendConsoleCommand("", command);
        } catch (NotesException var7) {
            var7.printStackTrace();
        } finally {
            recycle(session);
        }

    }

    public static String getDataDirectory(Session session) {
        try {
            return session.getEnvironmentString("Directory", true);
        } catch (NotesException var2) {
            var2.printStackTrace();
            return null;
        }
    }

    public static String getServerDomain(Session session) {
        return evaluateReturnString(session, "@Domain");
    }

    public static String getHashPassword(Session session, String pwd) {
        return evaluateReturnString(session, "@HashPassword(\"" + pwd + "\")");
    }

    public static String getServerName(Session session) {
        return evaluateReturnString(session, "@ServerName");
    }

    public static String getServerCNName(Session session) {
        return evaluateReturnString(session, "@Name([CN];@ServerName)");
    }

    public static String evaluateReturnString(Session session, String formula) {
        Vector vector = evaludateReturnVector(session, formula);
        return vector != null && vector.size() != 0 ? (String) vector.get(0) : null;
    }

    public static String evaluateReturnString(Session session, String formula, Document doc) {
        Vector vector = evaludateReturnVector(session, formula, doc);
        return vector != null && vector.size() != 0 ? (String) vector.get(0) : null;
    }

    public static Vector evaludateReturnVector(Session session, String formula) {
        if (session == null) {
            return null;
        } else {
            Vector vector = null;

            try {
                vector = session.evaluate(formula);
            } catch (NotesException var4) {
                var4.printStackTrace();
            }

            return vector;
        }
    }

    public static Vector evaludateReturnVector(Session session, String formula, Document doc) {
        if (session == null) {
            return null;
        } else if (doc == null) {
            return null;
        } else {
            Vector vector = null;

            try {
                vector = session.evaluate(formula, doc);
            } catch (NotesException var5) {
                var5.printStackTrace();
            }

            return vector;
        }
    }

    public static Database openDatabase(Session session, String ip, String dbFilePath) {
        if (session == null) {
            return null;
        }

        if (StringUtils.isBlank(dbFilePath)) {
            return null;
        }
        if (StringUtils.isBlank(ip)) {
            ip = "";
        }

        Database db = null;

        try {
            db = session.getDatabase(ip, dbFilePath);
        } catch (NotesException var6) {
            System.out.println("can not open db " + dbFilePath);
        }

        if (db != null) {
            try {
                if (!db.isOpen()) {
                    recycle(db);
                    db = null;
                    System.out.println("can not open db " + dbFilePath);
                }
            } catch (NotesException e) {
                e.printStackTrace();
            }
        }

        return db;
    }

    public static boolean copyFile(Document sourceDoc,
                                   Document destDoc,
                                   Vector fileNames,
                                   String tmpPath,
                                   String richItemName) {
        if (sourceDoc == null) {
            return false;
        }

        if (destDoc == null) {
            return false;
        }

        if (fileNames == null) {
            return false;
        }
        if (StringUtils.isBlank(tmpPath)) {
            return false;
        }

        if (StringUtils.isBlank(richItemName)) {
            return false;
        }

        tmpPath = adjustPath(tmpPath);
        RichTextItem riItem = null;
        EmbeddedObject emObj = null;

        try {
            riItem = (RichTextItem) destDoc.getFirstItem(richItemName);
            if (riItem == null) {
                riItem = destDoc.createRichTextItem(richItemName);
            }
            Enumeration enumeration = fileNames.elements();
            while (enumeration.hasMoreElements()) {
                String fileName = (String) enumeration.nextElement();
                emObj = sourceDoc.getAttachment(fileName);
                if (emObj != null && emObj.getType() == 1454) {
                    emObj.extractFile(tmpPath + emObj.getSource());
                    riItem.embedObject(1454, (String) null, emObj.getSource(), emObj.getSource());
                    recycle(emObj);
                    File file = new File(tmpPath + emObj.getSource());
                    file.delete();
                }
            }
            return true;
        } catch (NotesException e) {
            e.printStackTrace();
        } finally {
            recycle(emObj);
            recycle(riItem);
        }
        return false;
    }

    private static String adjustPath(String tmpPath) {
        int length = tmpPath.length();
        String lastString = tmpPath.substring(length - 1, length);
        return !lastString.equals("\\") && !lastString.equals("/") ? tmpPath + "/" : tmpPath;
    }

    public static void recycle(Base base) {
        if (base != null) {
            try {
                base.recycle();
            } catch (NotesException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static String getItemValueString(Document doc, String itemName, String splitWord) {
        if (doc == null) {
            return "";
        }
        if (StringUtils.isBlank(itemName)) {
            return "";
        }
        String result = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Item item = null;
        try {
            item = doc.getFirstItem(itemName);
            if (item == null) {
                return "";
            }
            if (item.getValues() == null) {
                return "";
            }

            if (item.getType() == Item.TEXT) {
                return StringUtils.join(item.getValues(), splitWord);
            } else if (item.getType() == Item.RICHTEXT) {
                RichTextItem ritem = (RichTextItem) item;
                String text = ritem.getUnformattedText();
                recycle(ritem);
                return text;
            } else if (item.getType() == Item.NUMBERS) {
                Vector vector = item.getValues();
                List values = new ArrayList();
                Iterator iterator = vector.iterator();
                while (iterator.hasNext()) {
                    Double value = (Double) iterator.next();
                    if (value != null) {
                        values.add(String.valueOf(value));
                    } else {
                        values.add("");
                    }
                }
                return StringUtils.join(values, splitWord);
            } else if (item.getType() == Item.DATETIMES) {
                Vector vector = item.getValues();
                List values = new ArrayList();
                Iterator iterator = vector.iterator();
                DateTime datetime = null;
                while (iterator.hasNext()) {
                    datetime = (DateTime) iterator.next();
                    if (datetime != null) {
                        values.add(sdf.format(datetime.toJavaDate()));
                    } else {
                        values.add("");
                    }
                    recycle(datetime);
                }
                return StringUtils.join(values, splitWord);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            recycle(item);
        }
        return result;
    }

    public static String getItemValueString(Document doc, String itemName) {
        return getItemValueString(doc, itemName, DEFAULT_SPLIT_WORD);
    }

    public static Vector getAttachmentNames(Session session, Document doc) {
        Vector fileVector = new Vector();
        return session != null && doc != null ? evaludateReturnVector(session, "@AttachmentNames(@True)", doc) : fileVector;
    }
}
